package cn.koala.key.common.utils;

import cn.koala.key.common.enums.HasCaption;
import cn.koala.key.common.enums.HasCode;
import org.springframework.util.Assert;

/**
 * 枚举工具类
 *
 * @author liutao
 * @date 2021/1/14
 */
public abstract class BaseEnumUtil {

    public static <T extends HasCode> T getByCode(Integer code, Class<T> enumClass) {
        for (T each : enumClass.getEnumConstants()) {
            if (each.getCode().equals(code)) {
                return each;
            }
        }
        return null;
    }

    public static <T extends Enum & HasCaption> T getByCaption(Class<T> clazz, String caption) {
        Assert.notNull(clazz, "clazz");

        for (T item : clazz.getEnumConstants()) {
            if (item.getCaption().equals(caption)) {
                return item;
            }
        }
        return null;
    }
}